import java.util.Random;

public class BankersAlgorithm {
    // 安全性检查算法
    public static boolean isSafe(int[][] max, int[][] allocated, int[] available, int numProcesses, int numResources) {
        boolean[] finish = new boolean[numProcesses];
        int[] work = available.clone();
        int count = 0;

        while (count < numProcesses) {
            boolean found = false;
            for (int i = 0; i < numProcesses; i++) {
                if (!finish[i]) {
                    boolean possible = true;
                    for (int j = 0; j < numResources; j++) {
                        if (max[i][j] - allocated[i][j] > work[j]) {
                            possible = false;
                            break;
                        }
                    }
                    if (possible) {
                        for (int j = 0; j < numResources; j++) {
                            work[j] += allocated[i][j];
                        }
                        finish[i] = true;
                        found = true;
                        count++;
                    }
                }
            }
            if (!found) {
                return false;
            }
        }
        return true;
    }

    // 资源请求算法
    public static boolean requestResources(int[][] max, int[][] allocated, int[] available, int pid, int[] request, int numResources) {
        for (int i = 0; i < numResources; i++) {
            if (request[i] > max[pid][i] - allocated[pid][i] || request[i] > available[i]) {
                return false;
            }
        }
        for (int i = 0; i < numResources; i++) {
            available[i] -= request[i];
            allocated[pid][i] += request[i];
        }
        if (isSafe(max, allocated, available, max.length, numResources)) {
            return true;
        } else {
            // 恢复分配前状态
            for (int i = 0; i < numResources; i++) {
                available[i] += request[i];
                allocated[pid][i] -= request[i];
            }
            return false;
        }
    }

    public static void main(String[] args) {
        Random rand = new Random();
        int numProcesses = 5;
        int numResources = 3;

        // 初始化数据
        int[][] max = new int[numProcesses][numResources];
        int[][] allocated = new int[numProcesses][numResources];
        int[] available = new int[numResources];

        // 随机初始化最大需求矩阵
        for (int i = 0; i < numProcesses; i++) {
            for (int j = 0; j < numResources; j++) {
                max[i][j] = rand.nextInt(10) + 1;
            }
        }
        // 随机初始化可用资源向量
        for (int i = 0; i < numResources; i++) {
            available[i] = rand.nextInt(20) + 1;
        }

        // 模拟资源分配
        for (int i = 0; i < 10; i++) {
            int pid = rand.nextInt(numProcesses);
            int[] request = new int[numResources];
            for (int j = 0; j < numResources; j++) {
                request[j] = rand.nextInt(max[pid][j] - allocated[pid][j] + 1);
            }
            if (requestResources(max, allocated, available, pid, request, numResources)) {
                System.out.println("进程 " + pid + " 请求资源成功");
            } else {
                System.out.println("进程 " + pid + " 请求资源失败");
            }
        }
    }
}